package com.climber.android.commonres.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.climber.android.commonres.R
import com.climber.android.commonres.helper.PermissionReallyDeclinedImpl
import io.android.library.titlebar.SmartTitleBar
import com.climber.android.commonsdk.event.BusProvider
import com.climber.android.commonsdk.event.EmptyEvent
import io.ganguo.library.mvp.ui.mvp.IPresenter
import io.ganguo.library.mvp.ui.mvp.base.IBaseFragment
import io.ganguo.library.rxpermissions.RuntimePermissionUtil
import org.greenrobot.eventbus.Subscribe

/**
 * Created by Mikes at 2019-11-04 17:33
 */
abstract class BaseMVPFragment<P : IPresenter> : IBaseFragment<P>(){
    private var smartTitleBar: SmartTitleBar?=null
    protected fun getSmartToolBar() : SmartTitleBar? = smartTitleBar

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if(!BusProvider.getInstance().isRegistered(this)){
            BusProvider.getInstance().register(this)
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        findAndInitToolBar(view)
        super.onViewCreated(view, savedInstanceState)
    }

    private fun findAndInitToolBar(view: View){
        smartTitleBar = view.findViewById(R.id.qmi_title_bar)
        smartTitleBar?.let {
            it.setLeftViewClickListener(View.OnClickListener {
                activity?.onBackPressed()
            })
        }
    }

    protected fun hideNavigationIcon(){
        smartTitleBar?.let {
            it.leftView.visibility = View.GONE
        }
    }

    protected fun setToolBarTitle(titleResId : Int){
        smartTitleBar?.let {
            it.titleView.setText(titleResId)
        }
    }

    protected fun setToolBarTitle(title : String){
        smartTitleBar?.let {
            it.titleView.text = title
        }
    }

    protected fun setEmptyToolBarTitle(){
        smartTitleBar?.let {
            it.titleView.text = ""
        }
    }

    fun requestSinglePermission(permissionName: String, explanationMessage: String? = null,
                                iPermissionReallyDeclinedCallBack : ((permissionName: String) -> Unit) ?= PermissionReallyDeclinedImpl(),
                                iPermissionResultCallBack: (Boolean) -> Unit
    ){
        RuntimePermissionUtil(this).requestSinglePermission(permissionName,
            explanationMessage,
            object : RuntimePermissionUtil.RequestPermission{
                override fun onRequestPermissionFailureWithAskNeverAgain(permissionName: String) {
                    iPermissionReallyDeclinedCallBack?.invoke(permissionName)
                    iPermissionResultCallBack.invoke(false)
                }

                override fun onRequestPermissionSuccess() {
                    iPermissionResultCallBack.invoke(true)
                }

                override fun onRequestPermissionFailure(permissionName: String?) {
                    iPermissionResultCallBack.invoke(false)
                }
            })
    }

    override fun onDestroyView() {
        BusProvider.getInstance().unregister(this)
        super.onDestroyView()
    }

    @Subscribe
    fun onBusEvent(event: EmptyEvent) {

    }

}